package vashion.azeroth.core.service.impl.item;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.contant.AzerothConstants;
import vashion.azeroth.contant.ItemErrorEnum;
import vashion.azeroth.core.item.pojo.ItemSkuDO;
import vashion.azeroth.core.manager.item.BaseItemSkuManager;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.item.ItemSkuService;

import java.util.List;

/**
 * Created by liuhe on 15/10/4.
 */
@Service
public class ItemSkuServiceImpl implements ItemSkuService {

    private final static Logger log = LoggerFactory.getLogger(ItemSkuServiceImpl.class);

    @Autowired
    private BaseItemSkuManager baseItemSkuManager;

    public ApiResult<List<ItemSkuDO>> getItemSku(long itemId) {

        try {
            ItemSkuDO itemSkudo = new ItemSkuDO();
            itemSkudo.setItemId(itemId);
            itemSkudo.setPageSize(AzerothConstants.Item.MAX_SKU_COUNT);
            List<ItemSkuDO> itemSkuDOList = baseItemSkuManager.find(itemSkudo);
            return ApiResult.newSuccessResult(itemSkuDOList);
        } catch (Exception e) {
            log.error("ItemSkuServiceImpl.getItemSku error itemId=" + itemId, e);
            return ApiResult.newErrorResult(ItemErrorEnum.SYSTEM_ERROR.getErrorCode(), ItemErrorEnum.SYSTEM_ERROR.getErrorMessage());
        }
    }

    public ApiResult<ItemSkuDO> getSkuDetail(long skuId) {

        try {
            return ApiResult.newSuccessResult(baseItemSkuManager.get(skuId));
        } catch (Exception e) {
            log.error("ItemSkuServiceImpl.getSkuDetail error skuId=" + skuId, e);
            return ApiResult.newErrorResult(ItemErrorEnum.SYSTEM_ERROR.getErrorCode(), ItemErrorEnum.SYSTEM_ERROR.getErrorMessage());
        }
    }

    public ApiResult<Boolean> updateItemSkuNum(long skuId, int num, boolean isAdd) {
        try {
            return ApiResult.newSuccessResult(baseItemSkuManager.updateItemSkuNum(skuId, num, isAdd));
        } catch (Exception e) {
            log.error("ItemSkuServiceImpl.updateItemSkuNum error skuId=" + skuId, e);
            return ApiResult.newErrorResult(ItemErrorEnum.SYSTEM_ERROR.getErrorCode(), ItemErrorEnum.SYSTEM_ERROR.getErrorMessage());
        }
    }
}
